﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SecurityLog.Factory;
using SecurityLog.Models;
using SecurityLog.Repositories;

namespace SecurityLog.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class AdminController : Controller
    {
        public ActionResult Index()
        {
            AdminIndexModel m = new AdminIndexModel();

            UsersRepository r = new UsersRepository();
            
            IEnumerable<Models.User> users = r.GetAllUsers();
            m.EnabledUsers = users.Where(x => x.Enabled);
            m.DisabledUsers = users.Where(x => !x.Enabled);

            return View(m);
        }

        public ActionResult UserAdd()
        {
            return RedirectToAction("UserEdit", new { id = 0 });
        }

        public ActionResult UserEdit(int id)
        {

            AdminUserEditModel m = new AdminUserEditModel { Enabled = true };

            if (id > 0)
            {
                UsersRepository r = new UsersRepository();
                Models.User u = r.GetUserById(id);
                if (u != null)
                {
                    m = new AdminUserEditModel
                    {
                        Id = u.Id,
                        Name = u.Name,
                        Initials = u.Initials,
                        Enabled = u.Enabled,
                        CanDelete = u.Entries == null || u.Entries.Count == 0
                    };
                }
            }

            return View(m);
        }

        [HttpPost]
        public ActionResult UserEdit(int id, AdminUserEditModel m)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Biz.Users user = new Biz.Users(m.Name, m.Initials);
                    if (user.ValidateUserData())
                    {
                        UsersRepository usersRepos = new UsersRepository();
                        if (m.Id > 0)
                            usersRepos.EditUser(m.Id, m.Name, m.Initials, m.Password, m.Enabled);
                        else
                            usersRepos.CreateNewUser(m.Name, m.Initials, m.Password, m.Enabled);

                        return RedirectToAction("Index");
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex);
                }
            }

            return View(m);
        }

        public ActionResult UserDelete(int id)
        {
            var usersRepository = new UsersRepository();
            var user = usersRepository.GetUserById(id);

            return View(new AdminUserEditModel { Id = user.Id, Initials = user.Initials, Name = user.Name });
        }

        [HttpPost]
        public ActionResult UserDelete(FormCollection collection)
        {
            string strId = collection["Id"];
            int id;
            if (int.TryParse(strId, out id))
            {
                var user = UserFactory.GetUserFromRepository(id);
                if (!user.HasEntries)
                {
                    var usersRepository = new UsersRepository();
                    usersRepository.DeleteUser(id);
                }
                else
                    throw new Exception("Not allowed to delete user. Try to disable.");
            }

            return RedirectToAction("Index");
        }

        /*
        public ActionResult ToggleUser(int id)
        {
            UsersRepository r = new UsersRepository();
            r.ToggleUser(id);

            return Redirect(Request.UrlReferrer.OriginalString);
        }
        */

    }
}
